package evaluation.website;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class WebsiteValidatorCreator {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		
		String type = "training";
		
		String technique = "TEDWL";
		
		int limit = 200;
		
		int position = 0;
		
		String[] prefix = new String[]{/*"added","changed","removed","total"*/"added"};
		
		String indexFileName = "/proj/dbNoBackup/pjbarrio/Experiments/Wrappers/" + type;
		
		for (int typ = 0; typ < prefix.length; typ++) {
			
			String extractions = "/proj/dbNoBackup/pjbarrio/Experiments/Wrappers/" + technique + "/" + type + "/"; 
			
			String websiteFolder = "/proj/dbNoBackup/pjbarrio/Experiments/Wrappers/Evaluation/"+ type + "/" + technique+ "/" + prefix[typ] + "Check/";
			
			new File(websiteFolder).mkdirs();
			
			String Weight = "/proj/dbNoBackup/pjbarrio/Experiments/Wrappers/SizeSortedList/" + type + "/";
			
			String queryResults = "/proj/dbNoBackup/pjbarrio/Experiments/Wrappers/QueryResults/" + type + "/";
			
			File indexFile = new File(indexFileName);
			
			String line;
			
			BufferedReader br = new BufferedReader(new FileReader(indexFile));
			
			int processed = 0;
			
			List<Integer> indexes = new ArrayList<Integer>();
			
			while ((line=br.readLine())!=null){
				
				if (processed == limit)
					break;
				
				int index = Integer.valueOf(line);
				
				indexes.add(index);
				
				processed++;
			}

			for (int i = 0; i < indexes.size(); i++) {
				
				int index = indexes.get(i);
				
				File f = new File(Weight + index + ".list");			
				
				File theWebsite = getPositionFile(queryResults + index + "/",f,position);
				
				if (theWebsite == null){
					theWebsite = new File("empty.html");
				}
				
				File theExtraction = getExtractionPositionFile(extractions + index + "/", f, position, prefix[typ]);
				
				if (theExtraction == null){
					theExtraction = new File("empty.html");
				}
				
				generateWebPage(index,theWebsite.getAbsolutePath(),websiteFolder,getIndex(indexes,i-1),getIndex(indexes,i+1),theExtraction.getAbsolutePath());
				
			}
			
			br.close();
			
		}
		

	}

	private static File getExtractionPositionFile(String root, File f,
			int position, String prefix) throws IOException {
		
		BufferedReader br = new BufferedReader(new FileReader(f));
		
		String line;
		
		int currPos = 0;
		
		while((line=br.readLine())!=null){
			
			File ff = new File(root + generatePrefix(prefix) + line);
			
			if (ff.exists()){
				
				if (position == currPos){
					
					br.close();
					
					return ff;
					
				}
				
				currPos++;
			}
			
		}
		
		br.close();
		
		return null;
		
	}

	private static String generatePrefix(String prefix) {
		
		if (prefix == null || prefix.isEmpty())
			return "";
		return prefix + ".";
		
	}

	private static int getIndex(List<Integer> files, int i) {
		if (i==-1){
			i = files.size()-1;
		}
		if (i==files.size()){
			i = 0;
		}
		return Integer.valueOf(files.get(i));
	}
	
	private static File getPositionFile(String root, File f, int position) throws IOException {
		
		BufferedReader br = new BufferedReader(new FileReader(f));
		
		String line;
		
		int currPos = 0;
		
		while((line=br.readLine())!=null){
			
			File ff = new File(root + line);
			
			if (ff.exists()){
				
				if (position == currPos){
					
					br.close();
					
					return ff;
					
				}
				
				currPos++;
			}
			
		}
		
		br.close();
		
		return null;
		
	}
	private static void generateWebPage(int index, String website,
			String websiteFolder, int previous, int next,String extraction) throws IOException {
		
		BufferedWriter bw = new BufferedWriter(new FileWriter(new File(websiteFolder + index + ".html")));
		bw.write("<html>");
		bw.newLine();
		bw.write("<body>");
		bw.newLine();
		bw.write("<table border=\"1\" width=\"100%\" height=\"100%\">");
		bw.newLine();
		bw.write("<tr height=\"95%\">");
		bw.newLine();
		bw.write(getWebsiteObject(60,website));
		bw.newLine();
		bw.write(getWebsiteObject(40, extraction));
		bw.newLine();
		bw.write("</tr>");
		bw.newLine();
		bw.write("<tr height=\"5%\">");
		bw.newLine();
		bw.write("<td colspan=2>");
		bw.newLine();
		bw.write("<table border=\"2\" width=\"100%\" height=\"100%\">");
		bw.newLine();
		bw.write("</tr>");
		bw.newLine();
		bw.write("<td width=\"50%\" align=\"right\">");
		bw.newLine();
		bw.write("<form>");
		bw.newLine();
		bw.write(generateButton(previous,"Previous"));
		bw.newLine();
		bw.write("</form>");
		bw.newLine();
		bw.write("</td>");
		bw.newLine();
		bw.write("<td width=\"50%\">");
		bw.newLine();
		bw.write("<form>");
		bw.newLine();
		bw.write(generateButton(next,"Next"));
		bw.newLine();
		bw.write("</form>");
		bw.newLine();
		bw.write("</td>");
		bw.newLine();
		bw.write("</tr>");
		bw.newLine();
		bw.write("</table>");
		bw.newLine();
		bw.write("</td>");
		bw.newLine();
		bw.write("</tr>");
		bw.newLine();
		bw.write("</table>");
		bw.newLine();
		bw.write("</body>");
		bw.newLine();		
		bw.write("</html>");
		bw.newLine();		
			
		bw.close();
		
	}

	private static String generateButton(int i, String text) {
		
		return "<input type=\"button\" onClick=\"parent.location='"+ i + ".html'\" value=\""+ text +"\">";
		
	}

	private static String getWebsiteObject(int percent, String website) {
		
		String ret ="";
		
		ret = "<td width=\"" +percent +"%\"><frame><object data=";
		
		ret = ret + website;
		
		ret = ret + " width=\"100%\" height=\"100%\"> <embed src=";
		
		ret = ret + website;
		
		ret = ret + " width=\"100%\" height=\"100%\"> </embed> Error: Embedded data could not be displayed. </object></frame></td>";
		 
		return ret;
		
	}

}